package com.yypinger.gxonline.view.basedata;

import org.vaadin.appfoundation.i18n.Lang;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalSplitPanel;
import com.yypinger.gxonline.ActiveLink;
import com.yypinger.gxonline.ActiveLink.LinkActivatedEvent;
import com.yypinger.gxonline.ActiveLink.LinkActivatedListener;
import com.yypinger.gxonline.GxOnlineApplication;
import com.yypinger.gxonline.entities.User;
import com.yypinger.gxonline.view.basedata.usermanager.UserFormWindow;

public class UserManagerView extends VerticalSplitPanel implements LinkActivatedListener , ItemClickListener{
	
	//用户列表
	private Table userList = new Table();
	
	//用户充值记录
	private Table userRechangeList;
	
	private BeanItemContainer<User> dataSource = new BeanItemContainer<User>(User.class);
	
	/**
	 * 构造函数
	 */
	public UserManagerView(){
		setSizeFull();
		setStyleName("navigationwarp");
		Navigation n = new Navigation(this);
		setLocked(true);
		setSplitPosition(90,Sizeable.UNITS_PIXELS);
		setFirstComponent(n);
		initUserTable();
		setSecondComponent(userList);
	}
	
	
	private void initUserTable(){ 
		// size
		
		userList.setSizeFull();
		userList.setSelectable(true);
		userList.setImmediate(true); // react at once when something is selected
		
		userList.addContainerProperty("code", String.class,null , "会员编号", null , null);
		userList.addContainerProperty("userName", String.class,null , "会员名称", null , null);
		userList.addContainerProperty("level", String.class,null , "会员级别", null , null);
		userList.addContainerProperty("discount", Double.class,null , "会员折扣", null , null);
		userList.addContainerProperty("customAmount", String.class,null , "消费总额", null , null);
		userList.addContainerProperty("point", Integer.class,null , "消费积分", null , null);
		userList.setContainerDataSource(dataSource);
		//添加鼠标点击监听
		userList.addListener(this);
	}
	
	 
	
	private class Navigation extends HorizontalLayout{
		
		//private Button searchBtn;
		private ActiveLink searchBtn;
		private ActiveLink addUserBtn;
		private ActiveLink modifyUserBtn;
		private ActiveLink deleteUserBtn;
		private ActiveLink settingUserBtn;
		private Field usercodeField;
		private Button expBtn;
		private Button impBtn;
		private Button reChargeBtn;
		private Button reCardBtn;
		
		public Navigation(UserManagerView userManagerView){
			setMargin(true);
			setSpacing(true);
			this.setStyleName("navigation");
			searchBtn =  new ActiveLink( Lang.getMessage("search"), new ExternalResource("#"));
			searchBtn.setIcon(new ThemeResource("../"+GxOnlineApplication.GXONLINE_THEME_NAME+"/png/40x40/search.png"));
			searchBtn.addListener(userManagerView);
			searchBtn.setData(userManagerView);
			
			
			addUserBtn = new ActiveLink(Lang.getMessage("add"),new ExternalResource("#"));
			addUserBtn.setIcon(new ThemeResource("../"+GxOnlineApplication.GXONLINE_THEME_NAME+"/png/40x40/User_add.png"));
			addUserBtn.addListener(userManagerView);
			
			modifyUserBtn = new ActiveLink(Lang.getMessage("modify") , new ExternalResource("#"));
			modifyUserBtn.setIcon(new ThemeResource("../"+GxOnlineApplication.GXONLINE_THEME_NAME+"/png/40x40/Tools.png"));
			
			deleteUserBtn = new ActiveLink(Lang.getMessage("delete") , new ExternalResource("#"));
			deleteUserBtn.setIcon(new ThemeResource("../"+GxOnlineApplication.GXONLINE_THEME_NAME+"/png/40x40/User_delete.png"));
			deleteUserBtn.addListener(userManagerView);
			
			settingUserBtn = new ActiveLink(Lang.getMessage("setting") , new ExternalResource("#"));
			settingUserBtn.setIcon(new ThemeResource("../"+GxOnlineApplication.GXONLINE_THEME_NAME+"/png/40x40/Settings.png"));
			settingUserBtn.addListener(userManagerView);
			
			usercodeField = new TextField(Lang.getMessage("system.searchUserLabel"));
			expBtn = new Button(Lang.getMessage("export"));
			impBtn = new Button(Lang.getMessage("import"));
			reChargeBtn = new Button(Lang.getMessage("recharge"));
			reCardBtn = new Button(Lang.getMessage("search"));
			
			addComponent(usercodeField);
			addComponent(searchBtn);
			addComponent(addUserBtn);
			addComponent(modifyUserBtn);
			addComponent(deleteUserBtn);
			addComponent(settingUserBtn);
			
		}
	}

	@Override
	public void linkActivated(LinkActivatedEvent event) {
		ActiveLink source = (ActiveLink)event.getSource();
		UserFormWindow uw =new UserFormWindow("创建会员");
		getWindow().addWindow(uw);
		uw.center();
		uw.setHeight("300px");
		uw.setWidth("500px");
	}


	@Override
	public void itemClick(ItemClickEvent event) {
		if (event.getButton() == ItemClickEvent.BUTTON_RIGHT) {
           //右键
        }

        if (event.isDoubleClick()) {
        	//双击
        	Object itemId = event.getItemId();
            Object propertyId = event.getPropertyId();
        }
		
	}
	
}
